/**
 * Copyright 2020-2030 Jinhui Technology Co. LTD  All Rights Reserved.
 */
package jgl.vajra.management.netty;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import io.netty.buffer.PooledByteBufAllocator;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import jgl.vajra.management.manager.AccountManager;
import jgl.vajra.management.netty.gson.IntegerNullAdapter;
import jgl.vajra.management.netty.gson.LongNullAdapter;
import jgl.vajra.management.netty.gson.StringNullAdapter;
import jgl.vajra.management.widget.base.BaseApplication;
import jgl.vajra.management.widget.retrofithelper.utils.AEACCoder;
import jgl.vajra.management.widget.utils.PhoneUtils;
import jgl.vajra.management.widget.utils.UIUtils;

/**
 * . <br>
 *
 * @author nongkegai<br>
 * @version <br>
 * @date Create in 2021/11/5 17:21 <br>
 * @description 1.0 <br>
 */


public class ConnectionListener implements ChannelFutureListener {

    private static final Gson gson = new GsonBuilder()
            .registerTypeAdapter(String.class, new StringNullAdapter())
            .registerTypeAdapter(Integer.class, new IntegerNullAdapter())
            .registerTypeAdapter(Long.class, new LongNullAdapter()).create();

    private AEACCoder aesCCoderUtil;
    private ImConnection imConnection;
    private final PooledByteBufAllocator allocator = new PooledByteBufAllocator(false);

    public ConnectionListener(ImConnection imConnection, AEACCoder aesCCoderUtil) {
        this.imConnection = imConnection;
        this.aesCCoderUtil = aesCCoderUtil;
    }

    @Override
    public void operationComplete(ChannelFuture channelFuture) throws Exception {
        if (channelFuture.isSuccess()) {
            System.out.println("服务端链接成功，准备登录...");
            if (channelFuture.channel().isWritable()) {
                //登录
                long start = System.nanoTime();
                login(channelFuture.channel());
                long end = System.nanoTime() - start;
                System.out.println("登录用时: " + end);
            }
        }
    }

    /**
     * 登录
     *
     * @param channel
     */
    private void login(Channel channel) {
        if (channel.isActive() && channel.isWritable()) {
            try {

                BaseApplication.setNettyChannel(channel);

                MessageInfo userInfo = new MessageInfo();
                userInfo.type = 1;
                userInfo.userType = AccountManager.getInstance().getLoginData(UIUtils.getContext()).userType;
                userInfo.invitation = AccountManager.getInstance().getLoginData(UIUtils.getContext()).invitation;
                userInfo.token = AccountManager.getInstance().getLoginData(UIUtils.getContext()).authorization;
                userInfo.udid = PhoneUtils.getUDID();
                System.out.println(userInfo.token + " ==== userInfo.invitation登录==" + userInfo.invitation);
                //String jsonStr = JSON.toJSONString(userInfo);
                String jsonStr = gson.toJson(userInfo);
                System.out.println("登录: " + jsonStr);
                ChannelFuture f = channel.writeAndFlush(jsonStr);
                f.addListener(new writeListener());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
